﻿#include "dialog_485_setting.h"
#include "plug_info.h"
#include "ui_dialog_485_setting.h"

#include <nuf_bus_info.h>
#include <com_manager.h>




#ifdef Q_CC_MSVC
#pragma execution_character_set("utf-8")
#endif





Dialog485Setting::Dialog485Setting(int port, int board, QWidget* parent)
	: QDialog(parent)
	, ui(new Ui::Dialog485Setting)
	, port_id(port)
	, board_id(board)
{
	ui->setupUi(this);

	this->initWnd();

	this->setWindowTitle("485 设置：" + QString("node%1_p%2").arg(board).arg(port));
	//	this->setWindowTitle(QString("485"));
	//	this->resize(960, 500);


}

Dialog485Setting::~Dialog485Setting()
{
	delete ui;
}

void Dialog485Setting::initWnd()
{
	// ReceiveCount = 0;
	// SendCount = 0;
	// IsShow = true;
	// IsAutoClear = false;
	// IsHexSend = true;
	// IsHexReceive = true;
	// IsDebug = false;

    QStringList comList;//串口号
	QStringList baudList;//波特率
	QStringList parityList;//校验位
	QStringList dataBitsList;//数据位
	QStringList stopBitsList;//停止位


    // enumerator = new QextSerialEnumerator(this);
    // enumerator->setUpNotifications();

    // connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
    // connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
    // QList<QextPortInfo> portList = QextSerialEnumerator::getPorts();
    foreach(QString info, com_manager::instance()->comList)
	{
        if (info.isEmpty() != false)
            comList.append(info);
	}

	ui->cboxPortName->addItems(comList);

	baudList << "1200" << "1800" << "2400" << "4800" << "9600"
		<< "19200" << "38400" << "56000" << "57600"
		<< "115200" << "128000" << "921600";

	ui->cboxBaudRate->addItems(baudList);

	parityList << "无" << "奇" << "偶";

#ifdef Q_OS_WIN//如果是windows系统
	parityList << "标志";
#endif

	parityList << "空格";

	ui->cboxParity->addItems(parityList);
	dataBitsList << "5" << "6" << "7" << "8";
	ui->cboxDataBit->addItems(dataBitsList);

	stopBitsList << "1";

#ifdef Q_OS_WIN//如果是windows系统
	stopBitsList << "1.5";
#endif

	stopBitsList << "2";

	ui->cboxStopBit->addItems(stopBitsList);


    QString node =  NufBusSetting::map_bus[board_id-1].p_list[port_id-1];
    _485 p =  PlugInfo::map_485[node] ;
    ui->cboxPortName->setCurrentText(p.com );//串口号
    ui->cboxBaudRate->setCurrentText(p.baud);//波特率
    ui->cboxParity->setCurrentText(p.parity);//校验位
    ui->cboxDataBit->setCurrentText(p.dataBit);//数据位
    ui->cboxStopBit->setCurrentText(p.stopBit);//停止位


}

void Dialog485Setting::on_button_ok_clicked()
{
    _485 p;
    p.  com =ui->cboxPortName->currentText() ;//串口号
    p.  baud = ui->cboxBaudRate->currentText();//波特率
    p.  parity = ui->cboxParity->currentText();//校验位
    p.dataBit = ui->cboxDataBit->currentText();//数据位
    p.  stopBit = ui->cboxStopBit->currentText();//停止位

    QString node =  NufBusSetting::map_bus[board_id-1].p_list[port_id-1];
    PlugInfo::map_485[node]  = p;
    PlugInfo::save_port_Setting() ;

	accept();
}


void Dialog485Setting::on_button_cancel_clicked()
{
	reject();
}

